﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using FellowshipOne.API.Groups.Model;
using FellowshipOne.API.Model;

namespace FellowshipOne.API.Events.Model
{
    [Serializable]
    [XmlRoot("attendanceSummary")]
    public class AttendanceSummary : ApiModel
    {
        private bool _isPosted;
        private bool _isScheduled;
        private bool _met;
        private int _absentCount;
        private int _presentCount;
        private DateTime _endDateTime;
        private DateTime _startDateTime;
        private string _attendanceContextValueId;
        private string _updatedDateString = string.Empty;
        private string _comments;

        public AttendanceSummary()
        {
            AttendanceContextValueID = String.Empty;
            Comments = String.Empty;
            UpdatedDateString = String.Empty;
        }

        

        private AttendanceContextType _attendanceContextType = new AttendanceContextType();
        private DateTime _createdDate;
        private DateTime _updatedDate;

        [XmlElement("attendanceContextType")]
        public AttendanceContextType AttendanceContextType
        {
            get { return _attendanceContextType; }
            set { _attendanceContextType = value; }
        }
        [XmlElement("attendanceContextValueID")]
        public string AttendanceContextValueID
        {
            get { return _attendanceContextValueId; }
            set { _attendanceContextValueId = value; }
        }

        [XmlElement("startDateTime")]
        public DateTime StartDateTime
        {
            get { return _startDateTime; }
            set { _startDateTime = value; }
        }

        [XmlElement("endDateTime")]
        public DateTime EndDateTime
        {
            get { return _endDateTime; }
            set { _endDateTime = value; }
        }

        [XmlElement("presentCount")]
        public int PresentCount
        {
            get { return _presentCount; }
            set { _presentCount = value; }
        }

        [XmlElement("absentCount")]
        public int AbsentCount
        {
            get { return _absentCount; }
            set { _absentCount = value; }
        }

        [XmlElement("totalCount")]
        public int TotalCount { get; set; }

        [XmlElement("met")]
        public bool Met
        {
            get { return _met; }
            set { _met = value; }
        }

        [XmlElement("isScheduled")]
        public bool IsScheduled
        {
            get { return _isScheduled; }
            set { _isScheduled = value; }
        }

        [XmlElement("isPosted")]
        public bool IsPosted
        {
            get { return _isPosted; }
            set { _isPosted = value; }
        }

        [XmlElement("comments")]
        public string Comments
        {
            get { return _comments; }
            set { _comments = value; }
        }

        [XmlElement("createdDate")]
        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set { _createdDate = value; }
        }

        [XmlIgnore]
        public DateTime? UpdatedDate
        {
            get
            {
                DateTime updateDate = DateTime.MinValue;

                if (DateTime.TryParse(_updatedDateString, out updateDate))
                {
                    return updateDate;
                }

                return null;
            }
            set
            {
                if (value.HasValue)
                {
                    _updatedDateString = value.Value.ToString();
                }
                else
                {
                    _updatedDateString = string.Empty;
                }
            }
        }

        [XmlElement("updatedDate")]
        public string UpdatedDateString
        {
            get
            {
                if (!string.IsNullOrEmpty(_updatedDateString))
                {
                    DateTime dt = DateTime.Now;

                    if (DateTime.TryParse(_updatedDateString, out dt))
                    {
                        _updatedDateString = dt.ToString("s");
                    }
                }

                return _updatedDateString;
            }
            set
            {
                if (value != null)
                {
                    _updatedDateString = value;
                }
            }
        }
    }

    [Serializable]
    [XmlRoot("results")]
    public class AttendanceSummaryCollection : Collection<AttendanceSummary>
    {
        public AttendanceSummaryCollection()
        {
        }

        public AttendanceSummaryCollection(List<AttendanceSummary> attendanceSummaries)
        {
            if (attendanceSummaries != null)
            {
                this.AddRange(attendanceSummaries);
            }
        }

    }

}
